// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import SaveManager from "../../Managers/SaveManager";
import PlatformManager from "../../Platforms/PlatformManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NotIndulgeView extends cc.Component {

    @property(cc.EditBox)
    editBox: cc.EditBox = null;
    @property(cc.EditBox)
    realNameEdit: cc.EditBox = null;
    @property(cc.Button)
    identificationBtn: cc.Button = null;
    @property(cc.Label)
    tipsTxt: cc.Label = null;

    protected onEnable(): void {
        this.tipsTxt.node.active = false;
    }

    async identificationClick() {
        var txt = this.editBox.string;
        var realName = this.realNameEdit.string;
        this.identificationBtn.interactable = false;
        let result = await PlatformManager.single.basePlatform.sdk.fcm(SaveManager.single.accountData.username, realName, txt);

        if (result.code == 200) {
            SaveManager.single.age = result.data.age;
            console.log("认证年龄：" + SaveManager.single.age);
            console.log('防沉迷认证成功', result);
            this.node.active = false;
        } else {
            this.identificationBtn.interactable = true;
            this.showTips(result.message);
            console.log('防沉迷认证失败', result);
        }
    }

    showTips(str: string) {
        this.tipsTxt.node.active = true;
        this.tipsTxt.string = str;
        this.scheduleOnce(() => {
            this.tipsTxt.node.active = false;
        }, 2);
    }
}
